<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>

<%@ page import="java.util.List" %>
<%@ page import="com.liferay.portal.kernel.dao.orm.DynamicQuery" %>
<%@ page import="com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil" %>
<%@ page import="com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil" %>
<%@ page import="com.liferay.portal.kernel.exception.SystemException" %>
<%@ page import="com.liferay.portal.kernel.util.PortalClassLoaderUtil" %>
<%@ page import="com.liferay.portlet.journal.model.JournalArticle" %>
<%@ page import="com.liferay.portlet.journal.model.JournalStructure" %>
<%@ page import="com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil" %>
<%@ page import="com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil" %>
<%@ page import="com.liferay.portal.util.PortalUtil" %>
<%@ page import="com.liferay.portal.kernel.util.HtmlUtil" %>

<portlet:defineObjects />
<liferay-theme:defineObjects />

<div id="box-novedades" class="gradient-1 shadow-1">
	<div class="box-slider" id="rotator">
		<ul id="sli">
<%
    HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));
    String nota = httpReq.getParameter("nota");

    String currentURL = PortalUtil.getCurrentURL(request);

    if (nota == null || nota.trim().equals("")) {

        DynamicQuery query1 = DynamicQueryFactoryUtil.forClass(JournalStructure.class, PortalClassLoaderUtil.getClassLoader());
        query1.add(PropertyFactoryUtil.forName("name").like("%Noticia Slider%"));

        String structureId = null;

        try {
            List<JournalStructure> listJournalStructure = JournalStructureLocalServiceUtil.dynamicQuery(query1);
            for (JournalStructure journalStructure : listJournalStructure) {
                structureId = journalStructure.getStructureId();
            }
        } catch (SystemException e) {
            e.printStackTrace();
        }

        DynamicQuery query2 = DynamicQueryFactoryUtil.forClass(JournalArticle.class, PortalClassLoaderUtil.getClassLoader());
        query2.add(PropertyFactoryUtil.forName("structureId").eq(structureId));

        try {
            List<JournalArticle> listJournalArticle = JournalArticleLocalServiceUtil.dynamicQuery(query2);

            for (JournalArticle journalArticle : listJournalArticle) {
                out.write("<li>");
                String journalArticleTitle = journalArticle.getTitle("es_ES");
                String notaURL = currentURL + "?nota=" + journalArticleTitle;
                out.write("<a href=\"" + notaURL + "\"  style=\"text-decoration: none;\">");
                out.write("<div class=\"slider_item\">");
                out.write(JournalArticleLocalServiceUtil.getArticleContent(journalArticle.getGroupId(),journalArticle.getArticleId(),journalArticle.getTemplateId(),"VIEW","es_ES",themeDisplay));
                out.write("</div>");
                out.write("</a>");
                out.write("</li>");
            }
        } catch (SystemException e) {
            e.printStackTrace();
        }

    }
    else {
        DynamicQuery queryJournalArticle = DynamicQueryFactoryUtil.forClass(JournalArticle.class, PortalClassLoaderUtil.getClassLoader());
        queryJournalArticle.add(PropertyFactoryUtil.forName("title").like("%" + nota + "%"));

        try {
            List<JournalArticle> listJournalArticle = JournalArticleLocalServiceUtil.dynamicQuery(queryJournalArticle);

            for (JournalArticle journalArticle : listJournalArticle) {
                if (journalArticle.getTitle("es_ES").equals(nota)) {
                    out.write(journalArticle.getTitle("es_ES"));
                    out.write(JournalArticleLocalServiceUtil.getArticleContent(journalArticle.getGroupId(),journalArticle.getArticleId(),journalArticle.getTemplateId(),"VIEW","es_ES",themeDisplay));
                }
            }
        } catch (SystemException e) {
            e.printStackTrace();
        }

    }
%>
		</ul>
	</div>
</div>
